package com.alpha.servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.TreeSet;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.HttpConstraint;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.alpha.bom.Exam;
import com.alpha.bom.Professor;
import com.alpha.bom.Question;
import com.alpha.dao.ExamDao;
import com.alpha.dao.QuestionDao;

/**
 * Servlet implementation class AddQuestionServlet
 */
@WebServlet("/AddQuestionsServlet")
@ServletSecurity(@HttpConstraint(rolesAllowed = {"admin"}))
public class AddQuestionsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private static Logger logger = Logger.getLogger(AddQuestionsServlet.class.getName());
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddQuestionsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		logger.info(this.getClass().getSimpleName()+" get method started.");
		
		Professor professor = null;
		TreeSet<Question> questions = new TreeSet<Question>();
		
		HttpSession session = request.getSession();
		
		String category = "";
		
		if(session.getAttribute("user")!=null){
			professor = (Professor) session.getAttribute("user");
			ExamDao eDao = new ExamDao();
			if(request.getParameter("exam_id")!=null){
				
				int id = Integer.parseInt(request.getParameter("exam_id"));
				try {
					if(eDao.retrieveExamById(id)!=null){
						Exam exam = eDao.retrieveExamById(id);
						request.setAttribute("exam", exam);
						category = exam.getCategory();
					}
				} catch (NamingException e) {
					logger.error(e.getMessage());
					return;
				} catch (SQLException e) {
					logger.error(e.getMessage());
					return;
				}
			}
						
			QuestionDao qDao = new QuestionDao();
			
			try {
				for(Question question: qDao.retrieveAllQuestions()){
					if(category.equalsIgnoreCase(question.getCategory())){
						questions.add(question);
					}
				}
			} catch (Exception e) {
				logger.error(e.getMessage());
				return;
			}
			
			if(questions!=null){
				request.setAttribute("questions", questions);
			}
			
			RequestDispatcher dis=request.getRequestDispatcher("AddQuestions.jsp");
            dis.forward(request, response);
			return;
		}else{
			response.sendRedirect("Login.jsp");
			return;
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
